﻿using System;
using System.Collections.Generic;
using System.Web;

using EHSubmit.Model;
using EHSubmit.Util;

namespace EHSubmit.Logic.Services
{
    /// <summary>
    ///RemoteDataProxyImpl 的摘要说明
    /// </summary>
    public class RemoteDataProxyImpl : IRemoteDataProxy
    {
        public RemoteDataProxyImpl()
        {
        }

        /// <summary>
        /// 根据学生ID返回该生所上的课程信息
        /// </summary>
        /// <param name="sid">学生ID</param>
        /// <returns>课程信息</returns>
        public IList<Course> GetCoursesOfStudent(string sid)
        {
            try
            {
                Remote.DataProvider provider = new EHSubmit.Remote.DataProvider();
                string json = provider.GetCourseByStudent(sid);

                return DataConverter.ParseCourses(json);
            }
            catch (System.Net.WebException we)
            {
                LogHelper.Log("教务管理系统连接错误", we);

                throw new SysException("教务管理系统连接错误");
            }
        }

        /// <summary>
        /// 根据教师ID返回该教师所授的课程信息
        /// </summary>
        /// <param name="tid">教师信息</param>
        /// <returns>课程信息</returns>
        public IList<Course> GetCoursesOfTeacher(string tid)
        {
            try
            {
                Remote.DataProvider provider = new EHSubmit.Remote.DataProvider();
                string json = provider.GetCourseByTeacher(tid);

                return DataConverter.ParseCourses(json);
            }
            catch (System.Net.WebException we)
            {
                LogHelper.Log("教务管理系统连接错误", we);

                throw new SysException("教务管理系统连接错误");
            }
        }       

        /// <summary>
        /// 根据教师ID返回该教师所授的课程信息,不是课程班级信息
        /// </summary>
        /// <param name="tid">教师信息</param>
        /// <returns>课程信息</returns>
        public IList<BaseCourse> GetBaseCourseOfTeacher(string tid)
        {
            try
            {
                Remote.DataProvider provider = new EHSubmit.Remote.DataProvider();
                string json = provider.GetCourseTypeByTeacher(tid);

                return DataConverter.ParseBaseCourses(json);
            }
            catch (System.Net.WebException we)
            {
                LogHelper.Log("教务管理系统连接错误", we);

                throw new SysException("教务管理系统连接错误");
            }
        }

        /// <summary>
        /// 根据课程ID返回选了该课程的学生信息
        /// </summary>
        /// <param name="cid">学生信息</param>
        /// <returns>学生信息</returns>
        public IList<User> GetStudentsOfCourse(string cid)
        {
            try
            {
                Remote.DataProvider provider = new EHSubmit.Remote.DataProvider();
                string json = provider.GetStudentByCourse(cid);

                return DataConverter.ParseUsers(json);

            }
            catch (System.Net.WebException we)
            {
                LogHelper.Log("教务管理系统连接错误", we);

                throw new SysException("教务管理系统连接错误");
            }
        }
    }
}